import { showNotify } from 'vant'

export function copyText(text = '') {
  if (document.execCommand('copy')) {
    const input = document.createElement('input')
    input.setAttribute('value', text)
    document.body.appendChild(input)
    input.select()
    try {
      if (document.execCommand('copy')) {
        showNotify({ type: 'success', message: '复制成功' })
      }
    } catch (error) {
      showNotify({ type: 'warning', message: '复制失败' })
    } finally {
      document.body.removeChild(input)
    }
  } else if (navigator.clipboard && window.isSecureContext) {
    setTimeout(() => {
      navigator.clipboard.writeText(text).then(() => {
        showNotify({ type: 'success', message: '复制成功' })
      })
    }, 0)
  } else {
    showNotify({ type: 'error', message: '暂不支持复制' })
  }
}
